package com.pe.pgn.clubpgn.webapp.controller.consultabonomanual;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;


import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.pe.pgn.clubpgn.common.CLPConstantes;
import com.pe.pgn.clubpgn.domain.ClpbPromocionBono;
import com.pe.pgn.clubpgn.domain.ClpdTipoBono;
import com.pe.pgn.clubpgn.domain.ClpmBonoClientePersona;
import com.pe.pgn.clubpgn.service.BonoManualConstultarService;
import com.pe.pgn.clubpgn.service.PromocionBonoService;
import com.pe.pgn.clubpgn.service.TipoBonoService;
import com.pe.pgn.clubpgn.webapp.controller.BaseFormController;
import com.pe.pgn.clubpgn.webapp.util.CalendarUtil;

@Controller
@RequestMapping("/consultabonomanual/**")
public class ConsultabonomanualController extends BaseFormController {
	
	@Autowired
	private BonoManualConstultarService bonoConstultarService;

	@Autowired 
	private TipoBonoService tipoBonoService;
	
	@Autowired 
	private PromocionBonoService promocionBonoService;
	

	

	@RequestMapping(value = "/consultabonolist.html")
    public @ModelAttribute("consulbonosmanualesform") ClpmBonoClientePersona consultaBonosManuales(
        	@ModelAttribute("consulbonosmanualesform") ClpmBonoClientePersona consulbonomanual, 
        	@RequestParam(value = "search", required = false) String search, Model model) {
	
		List<ClpdTipoBono> listTipoBono = tipoBonoService.listarTiposBono(true);
		List<ClpmBonoClientePersona> listabonoconsul =null;
		
        if(StringUtils.isBlank(search)){
        	listabonoconsul = bonoConstultarService.consultarTodo();
        	
		}else{
			listabonoconsul = bonoConstultarService.ConsultarBonosManuales(consulbonomanual);
		}
		
        model.addAttribute("listabonoconsul", listabonoconsul);
    	model.addAttribute("consulbonosmanualesform", consulbonomanual);
        model.addAttribute("consulTipoBono", listTipoBono);
		return consulbonomanual;
    }
	
	
	@RequestMapping(value = "/consulbonosmanualesform.html", method = RequestMethod.GET)
	public @ModelAttribute("consulbonosmanualesform") ClpmBonoClientePersona verConsulBonoManual(
		   @RequestParam(value = "id", required = false) Long id, HttpServletRequest request, Model model) {
		
		ClpmBonoClientePersona bean  = new ClpmBonoClientePersona();
		List<ClpbPromocionBono> list = new ArrayList<ClpbPromocionBono>();
		
		try{
		
			list = promocionBonoService.listarPromocionesBono(true);
			
			if (id != null) { 
				bean = bonoConstultarService.obtenerBonoManual(id);
				
				for (ClpbPromocionBono clpbPromocionBono : list) {
					String idPromocionBono = clpbPromocionBono.getId().toString();
					
					if(bean.getCoPromocionBono().equals(idPromocionBono)){
						bean.setStPromocionActiva(true);
					}
				}
				
			}else{
				
				String fechaGeneracion = CalendarUtil.getDate(CLPConstantes.DATE_PATTERN);
				bean.setStBonoClientePersona(true);
				
				bean.setDeTipoBono("----");
				bean.setCoTipoBono("-1");
				bean.setFechaVencimiento("----");
				bean.setDeTipoGeneracion(CLPConstantes.DE_MANUAL);
				bean.setStTipoGeneracion(CLPConstantes.MANUAL);
				bean.setFechaGeneracion(fechaGeneracion);
			}		
		
		}catch(Exception e){
			saveError(request, getText("errors.general", request.getLocale()));
		}
		
		model.addAttribute("consulPromocionBono", list);
		model.addAttribute("consulbonosmanualesform", bean);
		return bean;
	}

	
	
}
